﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObject;
using BusinessLogic;
using Utility;
namespace WebTeamplate
{

    public partial class Admin_ChiTietTienTe : BasePage
    {
        #region Fields
        public string urlList = "TienTe.aspx";
        public string validateGroup = "subCate";
        #endregion

        #region ViewStates
        public string Command
        {

            get
            {
                object o = ViewState["Command"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["Command"] = value; }

        }

        #endregion

        #region Init,Loads
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientDefaultButton.SetPageDefaultButton(this.Page, btnSave.ClientID);

            if (!Page.IsPostBack)
            {

                CommonFunction.InitStatusPublishForComboboxDetail(drlPublish);
                InitParamater();
                InitTextControl();
                LoadData();
                drlPublish.Focus();
            }
        }

        private void LoadData()
        {
            Curency objData = CurencyUtil.Get(this.txtCode.Text);
            if (objData != null)
            {
                if (objData.IsPublished)
                    drlPublish.SelectedValue = "1";
                else drlPublish.SelectedValue = "0";
                txtName.Text = objData.Name;
               // txtCode.Text = objData.Code;
                txtCode.Enabled = false;
            }
        }

        #endregion

        #region Methos

        private void InitParamater()
        {
            txtCode.Text = CommonFunction.GetValueFromQueryString(this.Page, "code");
            this.Command = CommonFunction.GetValueFromQueryString(this.Page, "act");
            if (this.Command == string.Empty)
                this.Command = CommonFunction.DEFAULT_COMMAND_ADD;
        }

        private void InitTextControl()
        {

            btnSave.Text = Resources.Resource.LABEL_BUTTON_SAVE;
            btnCacel.Text = Resources.Resource.LABEL_BUTTON_CANCEL;
            btnReset.Text = Resources.Resource.LABEL_BUTTON_RESET;
            refvName.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblName));
            refvName.ValidationGroup = validateGroup;

            refvCode.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblCode));
            refvCode.ValidationGroup = validateGroup;

            refvPublish.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblPublish));
            refvPublish.ValidationGroup = validateGroup;
            vsumCategory.ValidationGroup = validateGroup;
            btnSave.ValidationGroup = validateGroup;
        }
        private void Update(Curency objData)
        {            
            if (!CurencyUtil.Update(objData))
                lblErrorMessage.Text = Resources.Resource.MESSAGE_UPDATE_ERROR;
            else
                Response.Redirect(urlList);
        }
        private void Insert(Curency objData)
        {
            int result=CurencyUtil.Insert(objData);
            if (result == 0)
                lblErrorMessage.Text = Resources.Resource.MESSAGE_INSERT_ERROR;
            else if (result == -1)
                lblErrorMessage.Text =string.Format(Resources.Resource.MESSAGE_EXISTS_ITEM,CommonFunction.GetTextLabel(lblCode));
            else
                Response.Redirect(urlList);
        }
        #endregion

        #region Events, Handler
        protected void drlLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.Command == CommonFunction.DEFAULT_COMMAND_EDIT)
                LoadData();

        }
        protected void btnCacel_Click(object sender, EventArgs e)
        {
            Response.Redirect(urlList);
        }
        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect(this.GetCurrentPageAndParamaterUrl());
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Curency objData = new Curency();
                if (drlPublish.SelectedValue == "1")
                    objData.IsPublished = true;
                else objData.IsPublished = false;              
                objData.Name = txtName.Text;
                objData.Code = txtCode.Text;
                objData.CreatedBy = objData.LastUpdatedBy = this.userLoginName;
                objData.CreatedDate = objData.LastUpdatedDate = CommonFunction.Now;
                if (this.Command == CommonFunction.DEFAULT_COMMAND_ADD)
                    Insert(objData);
                else
                    Update(objData);

            }
            else
            {
                vsumCategory.ShowMessageBox = false;
                vsumCategory.ShowSummary = true;
            }
        }
        #endregion
    }
}
